package com.xt.service.impl;

import com.xt.bean.Department;
import com.xt.dao.DepartmentRepository;
import com.xt.service.DepartmentService;
import com.xt.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * Created by XT on 2018/5/3.
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DepartmentRepository dr;

    @Override
    public void saveOrUpdateOne(Department d) {

        if (StringUtils.isEmpty(d.getId())) {
            //新增
            d.setCreateDate(new Date());
        }

        d.setUpdateDate(new Date());
        dr.save(d);
    }

    @Override
    public void deleteOne(String id) {
        //删除关联关系

        dr.delete(id);
    }

    @Override
    public Department getById(String id) {
        return dr.findOne(id);
    }

    @Override
    public Page<Department> getListByPage(int count, int pageNo, String sortDirection, String... sortField) {
        Page<Department> departmentPage = dr.findAll(PageUtil.getPageable(count,pageNo,sortDirection,sortField));
        return departmentPage;
    }

    @Override
    public Page<Department> getPageByName(String name, int count, int pageNo, String sortDirection, String... sortField) {
        Page<Department> departmentPage = dr.findByNameLike(name,PageUtil.getPageable(count,pageNo,sortDirection,sortField));
        return departmentPage;

    }



}
